package cn.ruituo.client;

import cn.ruituo.common.Request;
import cn.ruituo.common.Response;
import cn.ruituo.common.User;

import java.io.*;
import java.net.Socket;
import java.nio.file.Files;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class MainClient {
    public static void main(String[] args) {
        Socket s = null;
        try {
            s = new Socket("localhost", 8888);
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectOutputStream oout = new ObjectOutputStream(out);
            ObjectInputStream oin = new ObjectInputStream(in);

            int choice = 0;
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("1:login,2:register,3:download，4：上传,9 退出");
                choice = sc.nextInt();
                if (choice == 1) {
                    System.out.println("请输入用户和密码：");
                    String name = sc.next();
                    String pass = sc.next();
                    Request r = new Request("login");
                    r.put("userName", name);
                    r.put("userPass", pass);
                    //>>>
                    oout.writeObject(r);
                    oout.reset();
                    Response response = (Response) oin.readObject();
                    if (response.isSuccess()) {
                        User user = (User) response.get("loginUser");
                        System.out.println("原来你是：" + user.getRole());
                    } else {
                        System.out.println("登录失败！" + response.getMsg());
                    }
                    // System.out.println("结果是："+response.isSuccess()+",信息是："+response.getMsg());
                } else if (choice == 4) {
                    System.out.println("输入上传的路径：");
                    String fileName = sc.next();
                    Request request = new Request("upload");
                    File file = new File(fileName);
                    FileInputStream bin = new FileInputStream(file);
                    byte[] bb = new byte[bin.available()];
                    bin.read(bb);
                    bin.close();
                    request.put("novelname", file.getName());
                    request.put("content", bb);
                    oout.writeObject(request);
                    oout.reset();
                    Response response = (Response) oin.readObject();
                    System.out.println("结果是：" + response.isSuccess() + ",信息是：" + response.getMsg());

                }else if(choice==9){
                    Request request = new Request("quit");
                    oout.writeObject(request);
                    oout.reset();
                    Response response = (Response) oin.readObject();
                    //if(response.isSuccess())
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                s.close();
                System.out.println(s+"被关闭了。。。");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
